package com.example.selenium.video;

import cn.hutool.core.collection.CollUtil;
import com.example.selenium.bean.ChromeResponse;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.DesiredCapabilities;

import java.lang.reflect.Type;
import java.util.List;
import java.util.concurrent.TimeUnit;

public class VideoDemo {
    private static final Gson gson = new Gson();

    @lombok.SneakyThrows
    public static void main(String[] args) {

        WebDriver driver;
        // 初始化
        System.setProperty("webdriver.chrome.driver", "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe");
        ChromeOptions options = new ChromeOptions();
        options.addArguments("start-maximized");
//        DesiredCapabilities capabilities = DesiredCapabilities.chrome();
//        capabilities.setCapability(ChromeOptions.CAPABILITY, options);

        driver = new ChromeDriver();
        // 最大化浏览器
        driver.manage().window().maximize();
        // 打开网页
        driver.get("https://test.tairen.net/zixuepai-static/111.html");
//        driver.get("www.baidu.com");
        // 等待3秒
        driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        // 点击outter元素
        driver.findElement(By.className("outter")).click();

        while (true) {
            Thread.sleep(1000);
            String scriptToExecute = "var performance = window.performance || window.mozPerformance || window.msPerformance || window.webkitPerformance || {}; var network = performance.getEntries() || {}; return JSON.stringify(network);";

            String json = ((JavascriptExecutor) driver).executeScript(scriptToExecute).toString();

            Type type = new TypeToken<List<ChromeResponse>>() {
            }.getType();

            List<ChromeResponse> responses = gson.fromJson(json, type);
            if (CollUtil.isNotEmpty(responses)) {
                System.out.println("\n\n\n");
                responses
                        .stream()

                        .filter(chromeResponse -> {
                            String entryType = chromeResponse.getEntryType();
                            if ("resource".equals(entryType)) {
                                String name = chromeResponse.getName();
                                if (name.contains("https://v.source.zxxk.com")) {
                                    if (name.contains(".ts")) {
                                        return true;
                                    }
                                }
                                return false;
                            } else {
                                return false;
                            }
                        })
                        .map(chromeResponse -> {
                            return chromeResponse.getName();
                        })
                        .distinct()
                        .forEach(url -> {
                            VideoConsumer videoConsumer = new VideoConsumer(url);
                            new Thread(videoConsumer).start();
                        });
            }
        }

    }

}
